package com.novunda.platform.common.security;


import com.google.common.collect.Maps;
import com.novunda.platform.entities.User;

import java.io.Serializable;
import java.util.Map;

/**
 * 授权用户信息
 */
public class Principal implements Serializable {

    private static final long serialVersionUID = 1L;

    private final Long id;
    private String email;
    private final String username;
    private final String name;
    private String avatar;
    private Map<String, Object> cacheMap;

    public Principal(User user) {
        id = user.getId();
        username = user.getLoginName();
        email = user.getEmail();
        name = user.getRealName();
        avatar = user.getAvatar();
    }

    public Principal(Long id, String username, String name) {
        this.id = id;
        this.username = username;
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public String getName() {
        return name;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public Map<String, Object> getCacheMap() {
        if (cacheMap == null) {
            cacheMap = Maps.newHashMap();
        }
        //noinspection ReturnOfCollectionOrArrayField
        return cacheMap;
    }

    public String getEmail() {
        return email;
    }

    @Override
    public String toString() {
        return name;
    }
}
